package JasonsClasses;

import java.awt.*;

public class GameBoard {
	//private int ships;
	private GridPoint[][] gridPoints;
	private int height, width;
	private Ship[] shipArray = new Ship[5];
	public GameBoard(int height, int width){
		//this.ships = 0;
		int i, j;
		this.gridPoints = new GridPoint[width][height];
		this.height = height;
		this.width = width;
		for(i=0;i<height;i++){
			for(j=0;j<width;j++){
				gridPoints[i][j] = new GridPoint(i, j);
			}
		}
	}
	
	public GameBoard(){
		
	}
	
	public boolean isEmpty(int x, int y){
		return gridPoints[x][y].isEmpty();
	}
	
	public int getWidth(){
		return this.width;
	}
	
	public int getHeight(){
		return this.height;
	}
	
	public void clearSpace(int x, int y){
		this.gridPoints[x][y].setEmpty();
	}
	
	public void placeBoat(int x, int y, int length, boolean orientation, int index, int type){
		int i;
		for(i=0;i<length;i++){
			if(orientation){
				this.gridPoints[x][y + i].setFull(index);
			}
			else{
				this.gridPoints[x + i][y].setFull(index);
			}
		}
		this.shipArray[index] = new Ship(x, y, orientation, type);
	}
	
	public Graphics paintBoard(int x, int y, Graphics g, int size){
		int i, j;
		char[] columns = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'};
		String[] rows = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"};
		for(i=0;i<height;i++){
			g.setColor(Color.black);
			g.drawChars(columns, i, 1, x - (size/2), y + (size * i) + (int)(size / 1.4));
			for(j=0;j<width;j++){
				if(i==0){
					g.setColor(Color.black);
					g.drawString(rows[j], x + (size * j) + (int)(size * 0.4), y - (int)(size * 0.4));
				}
				if(gridPoints[i][j].isEmpty()){
					g.setColor(Color.cyan);
				}
				else if(gridPoints[i][j].isAttacked()){
					g.setColor(Color.red);
				}
				else{
					g.setColor(Color.black);
				}
				g.fillRect(x+(size*j), y+(size*i), size, size);
				g.drawRect(x+(size*j), y+(size*i), size, size);
			}
		}
		g.setColor(Color.black);
		for(i=1;i<height;i++){
			g.drawLine(x, y+(size*(i)), x+(width)*size, y+(size*(i)));
		}
		for(j=1;j<width;j++){
			g.drawLine(x+(size*(j)), y, x+(size*(j)), y+(height)*size);
		}
		return g;
	}
}
